<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

$pet_id = $_REQUEST["pet_id"];
//$pet_id = "16-2556";
//$panngan2 = $_REQUEST["panngan2"];
//$panngan3 = $_REQUEST["panngan3"];
//$ex1_id = $_REQUEST["ex1_id"];
//$ex2_id = $_REQUEST["ex2_id"];
//$ex3_id = $_REQUEST["ex3_id"];
//$month_year = $_REQUEST["month_year"];

$db = new myDBS;
$Cur = new Currency;

$sql = "SELECT DISTINCT tbl_salary.*, expenses1_name, expenses2_name, expenses3_name, panngan2_name"
        . " , panngan3_name, panngan2.panngan2_id, panngan3.panngan3_id, expenses_lvl3.e_id, verify_date, finance_id  "
        . " FROM tbl_salary, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3  "
        . " WHERE pet_id  = '$pet_id' "
        . " and expenses_lvl1.expenses1_id = ex1_id "
        . " and expenses_lvl2.expenses2_id = ex2_id "
        . " and expenses_lvl3.expenses3_id = ex3_id "
        . " and panngan2.panngan2_id = tbl_salary.panngan2_id "
        . " and panngan3.panngan3_id = tbl_salary.panngan3_id";
//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

$sql = "SELECT sum(salary) AS salary FROM tbl_salary WHERE pet_id  = '$pet_id' ";

$result1 = $db->runSQL($sql);
$rows1 = $db->fetch_object($result1);

$elass = gen_account_id($rows->panngan2_id, $rows->panngan3_id, $rows->e_id);

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->numPages = $this->setSourceFile('form/salary.pdf');

            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
$pdf = new PDF($orientation = 'P', $unit = 'mm', "A4", $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);
// now write some text above the imported page
$pdf->SetXY(30, 19);
//$pdf->Write(0, $_SESSION["lvl1"]);
$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");

$pdf->SetXY(32, 26);
$pdf->Write(0, $rows->budget_year);

$pdf->SetXY(94, 26);
$pdf->Write(0, $rows->pet_id);

$pdf->SetXY(162, 34 + $vertical_align);
if (!empty($rows->verify_date)) 
    $pdf->Write(0, date_format(date_create($rows->verify_date), "Y-m-d"));

$pdf->SetXY(24, 33.5);
$pdf->Write(0, $rows->panngan2_name);

$pdf->SetXY(84, 33.5);
$pdf->Write(0, $rows->panngan3_name);

$pdf->SetXY(157, 26 + $vertical_align);
$pdf->Write(0, $rows->finance_id);

$pdf->SetXY(28, 40.5);
//$pdf->Write(0, );
$pdf->MultiCell(50, 10, $rows->expenses1_name, 0, '', 0, 0, '', '', true);

$pdf->SetXY(90, 40.5);
$pdf->Write(0, $rows->expenses2_name);

$pdf->SetXY(160, 40.5);
$pdf->Write(0, $elass);

$pdf->SetXY(90, 47);
$pdf->Write(0, $rows->expenses3_name);

$pdf->SetXY(157, 47.5);
$pdf->Write(0, $elass);

$pdf->SetXY(77, 61);
$pdf->Cell(41, 0, number_format($rows1->salary, 2), 0, 0, 'R');

$pdf->SetXY(120, 61);
//$pdf->Write(0, $rows->comment, '', 0, 'J', true, 0, false, true, 0);
//$pdf->Cell(89,48, $rows->comment,0,	1,"L");
$pdf->MultiCell(90, 45, $_SESSION["lvl2"] . " ประจำ " . $rows->month_year, 0, '', 0, 1, '', '', true);

/*
  $pdf->SetXY(73, 76);
  $pdf->Cell(41,0,number_format($rows->inpValues * $rows->inpTax / 100, 2),0,0,'R');

  $pdf->SetXY(73, 83);
  $pdf->Cell(41,0,number_format($rows->inpTotal, 2),0,0,'R');

  $pdf->SetXY(73, 90);
  $pdf->Cell(41,0,number_format($rows->inpValues * $rows->WithholdingTax / 100, 2),0,0,'R');

  $pdf->SetXY(73, 97);
  $pdf->Cell(41,0,number_format($rows->inpPenalty, 2),0,0,'R');
 */
$pdf->SetXY(77, 99);
$pdf->Cell(41, 0, number_format($rows1->salary, 2), 0, 0, 'R');

$pdf->SetXY(56, 105);
$pdf->Write(0, $Cur->bahtThai($rows1->salary));


$pdf->SetXY(22, 159);
$pdf->Cell(39, 0, number_format($rows1->salary, 2), 0, 0, 'C');

$pdf->SetXY(90, 159);
$pdf->Cell(39, 0, number_format($rows1->salary, 2), 0, 0, 'C');

$pdf->SetXY(157, 159);
$pdf->Cell(39, 0, number_format($rows1->salary, 2), 0, 0, 'C');

$pdf->SetXY(29, 216);
$pdf->Cell(36, 0, number_format($rows1->salary, 2), 0, 0, 'C');

$pdf->SetXY(73, 216);
$pdf->Write(0, $Cur->bahtThai($rows1->salary));
/*
  $pdf->SetXY(27, 233);
  $pdf->Write(0, $rows->company_name);

  $pdf->SetXY(35, 247);
  $pdf->Cell(55,0,number_format($rows->inpNet, 2),0,0,'C');
 */

if ($pdf->numPages > 1) {
    for ($pages = 2; $pages <= $pdf->numPages; $pages++) {
        $pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($pages);
        $pdf->AddPage();

        switch ($pages) {
            case 2:
                $pdf->SetXY(30, 63);
                $pdf->Write(0, $_SESSION["lvl1"]);

                $pdf->SetXY(30, 74);
                $pdf->Write(0, $_SESSION["lvl2"] . " " . $_SESSION["lvl3"] . " " . $_SESSION["lvl4"]);

                $pdf->SetXY(30, 85);
                $pdf->Write(0, $rows->pet_id);

                $pdf->SetXY(70, 96);
                $pdf->Write(0, number_format($rows1->salary, 2));

                $pdf->SetXY(116, 130);
                $pdf->Cell(25, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                $pdf->SetXY(167, 130);
                $pdf->Cell(19, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                $pdf->SetXY(116, 182);
                $pdf->Cell(25, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                $pdf->SetXY(167, 182);
                $pdf->Cell(19, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                //$pdf->Write(0, number_format($rows1->salary,2));

                break;
            case 3:

                $pdf->SetXY(110, 27);
                $pdf->Write(0, $rows->pet_id);

                $pdf->SetXY(30, 37);
                $pdf->Write(0, $_SESSION["lvl2"]);

                $pdf->SetXY(105, 37);
                $pdf->Write(0, $_SESSION["lvl1"]);

                //$pdf->SetXY(105, 68);
                //$pdf->Cell(32,0,number_format($rows1->salary, 2),1,0,'R');


                $pdf->SetXY(80, 75);
                $pdf->Write(0, $rows->pet_id);

                $pdf->SetXY(105, 75);
                $pdf->Cell(32, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                $pdf->SetXY(175, 75);
                $pdf->Cell(32, 0, number_format($rows1->salary, 2), 0, 0, 'R');

                $sql = "SELECT * FROM tbl_salary WHERE pet_id  = '$pet_id' ";

                $result2 = $db->runSQL($sql);

                $db1 = NEW myDBS();
                $db1->selectDB("hr_udoncity");
                $i = 0;
                while ($rows2 = $db->fetch_object($result2)) {

                    $sql = "select * from person where id='" . $rows2->id . "'";
                    $result3 = $db1->runSQL($sql);
                    $rows3 = $db1->fetch_object($result3);
                    $pdf->SetXY(31, 123 + $i);
                    $pdf->Write(0, $rows3->fname . "   " . $rows3->lname);
                    $i+=7;
                }

                $i = 0;
                $pdf->SetXY(125, 123 + $i);
                $pdf->Cell(24, 0, number_format($rows->salary, 2), 0, 0, 'R');
                while ($rows = $db->fetch_object($result)) {
                    $i+=7;
                    $pdf->SetXY(125, 123 + $i);
                    $pdf->Cell(24, 0, number_format($rows->salary, 2), 0, 0, 'R');
                }

                break;
        }
    }
}

$pdf->Output('output_petition1.pdf', 'I');
?>